home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / tar.gnu / sprite / list.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-29  |  19.6 KB  |  854 lines

  1. /* List a tar archive.
  2.    Copyright (C) 1988 Free Software Foundation
  3.  
  4. This file is part of GNU Tar.
  5.  
  6. GNU Tar is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Tar is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Tar; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /*
  21.  * List a tar archive.
  22.  *
  23.  * Also includes support routines for reading a tar archive.
  24.  *
  25.  * this version written 26 Aug 1985 by John Gilmore (ihnp4!hoptoad!gnu).
  26.  *
  27.  * @(#)list.c 1.31 11/5/87 - gnu
  28.  */
  29. #include <stdio.h>
  30. #include <ctype.h>
  31. #include <string.h>
  32. #include <sys/types.h>
  33. #include <sys/stat.h>
  34. #ifndef    MSDOS
  35. #include <sys/file.h>
  36. #endif    /* MSDOS */
  37. #include <assert.h>
  38. #include <sys/param.h>
  39.  
  40. #ifdef USG
  41. #include <sys/sysmacros.h>    /* major() and minor() defined here */
  42. #endif
  43.  
  44. char *ctime();                /* From libc.a */
  45.  
  46. #define    isodigit(c)    ( ((c) >= '0') && ((c) <= '7') )
  47.  
  48. #include "tar.h"
  49. #include "port.h"
  50.  
  51. extern FILE *msg_file;
  52.  
  53. long from_oct();            /* Decode octal number */
  54. void demode();                /* Print file mode */
  55.  
  56. union record *head;            /* Points to current archive header */
  57. struct stat hstat;            /* Stat struct corresponding */
  58. int head_standard;            /* Tape header is in ANSI format */
  59.  
  60. void print_header();
  61. void skip_file();
  62. void skip_extended_headers();
  63.  
  64. void get_long_name();
  65. void get_short_name();
  66.  
  67. extern char *quote_copy_string();
  68.  
  69.  
  70. /*
  71.  * Main loop for reading an archive.
  72.  */
  73. void
  74. read_and(do_something)
  75.     void (*do_something)();
  76. {
  77.     int status = 3;            /* Initial status at start of archive */
  78.     int prev_status;
  79.     extern time_t new_time;
  80.  
  81.     name_gather();            /* Gather all the names */
  82.     open_archive(1);        /* Open for reading */
  83.  
  84.     for(;;) {
  85.         prev_status = status;
  86.         status = read_header();
  87.         switch (status) {
  88.  
  89.         case 1:            /* Valid header */
  90.             /* We should decode next field (mode) first... */
  91.             /* Ensure incoming names are null terminated. */
  92.             head->header.name[NAMSIZ-1] = '\0';
  93.             saverec(&head);    /* hold onto the header */
  94.             userec(head); /* and skip past it in the archive */
  95.             get_names(head);
  96.             if (   !name_match(current_filename)
  97.                 || (f_new_files && hstat.st_mtime<new_time)
  98.                 || (f_exclude && check_exclude(current_filename))) {
  99.                 /*
  100.                  * Skip past the header and file in the 
  101.                  * archive
  102.                  */
  103.                 if (gnu_extended_header(head)) {
  104.                     skip_extended_headers();
  105.                 }
  106.                 if(head->header.linkflag != LF_DIR)
  107.                     skip_file((long)hstat.st_size);
  108.                 saverec((union record **)0);
  109.                 continue;
  110.  
  111.             }
  112.  
  113.             (*do_something)();
  114.             saverec((union record **)0);
  115.             continue;
  116.  
  117.             /*
  118.              * If the previous header was good, tell them
  119.              * that we are skipping bad ones.
  120.              */
  121.         case 0:            /* Invalid header */
  122.             userec(head);
  123.             switch (prev_status) {
  124.             case 3:        /* Error on first record */
  125.                 msg("Hmm, this doesn't look like a tar archive.");
  126.                 /* FALL THRU */
  127.             case 2:        /* Error after record of zeroes */
  128.             case 1:        /* Error after header rec */
  129.                 msg("Skipping to next file header...\n");
  130.             case 0:        /* Error after error */
  131.                     /* abort(); */
  132.                 break;
  133.             }
  134.             continue;
  135.  
  136.         case 2:            /* Record of zeroes */
  137.             userec(head);
  138.             status = prev_status;    /* If error after 0's */
  139.             if (f_ignorez)    
  140.                 continue;
  141.             /* FALL THRU */
  142.         case EOF:        /* End of archive */
  143.             break;
  144.         }
  145.         break;
  146.     };
  147.  
  148.     close_archive();
  149.     names_notfound();        /* Print names not found */
  150. }        
  151.  
  152.  
  153. /*
  154.  * Print a header record, based on tar options.
  155.  */
  156. void
  157. list_archive()
  158. {
  159.     extern char *save_name;
  160.  
  161.     /* Print the header record */
  162.     if (f_verbose) {
  163.         if (f_verbose > 1)
  164.             decode_header(head, &hstat, &head_standard, 0);
  165.         print_header();
  166.     }
  167.  
  168.     if(f_gnudump && head->header.linkflag==LF_DUMPDIR) {
  169.         size_t    size, written, check;
  170.         char    *data;
  171.         extern int errno;
  172.         extern long save_totsize;
  173.         extern long save_sizeleft;
  174.  
  175.         if(f_multivol) {
  176.             save_name = current_filename;
  177.             save_totsize=hstat.st_size;
  178.         }
  179.         for(size = hstat.st_size;size>0;size-=written) {
  180.             if(f_multivol)
  181.                 save_sizeleft=size;
  182.             data = findrec()->charptr;
  183.             if(data==NULL) {
  184.                 msg("EOF in archive file?");
  185.                 break;
  186.             }
  187.             written = endofrecs()->charptr - data;
  188.             if(written>size)
  189.                 written=size;
  190.             errno=0;
  191.             check=fwrite(data,sizeof(char), written, msg_file);
  192.             userec((union record *)(data+written - 1));
  193.             if(check!=written) {
  194.                 msg_perror("only wrote %ld of %ld bytes to file %s",
  195.                        check, written,current_filename);
  196.                 skip_file((long)(size)-written);
  197.                 break;
  198.             }
  199.         }
  200.         if(f_multivol)
  201.             save_name = 0;
  202.         fputc('\n',msg_file);
  203.         fflush(msg_file);
  204.         return;
  205.  
  206.     }
  207.     /* Check to see if we have an extended header to skip over also */
  208.     if (gnu_extended_header(head))
  209.         skip_extended_headers();
  210.  
  211.     if(f_multivol) {
  212.         save_name = current_filename;
  213.     }
  214.  
  215.     /* Skip to the next header on the archive */
  216.  
  217.     skip_file((long) hstat.st_size);
  218.  
  219.     if(f_multivol)
  220.         save_name = 0;
  221. }
  222.  
  223.  
  224. /*
  225.  * Read a record that's supposed to be a header record.
  226.  * Return its address in "head", and if it is good, the file's
  227.  * size in hstat.st_size.
  228.  *
  229.  * Return 1 for success, 0 if the checksum is bad, EOF on eof,
  230.  * 2 for a record full of zeros (EOF marker).
  231.  *
  232.  * You must always userec(head) to skip past the header which this
  233.  * routine reads.
  234.  */
  235. int
  236. read_header()
  237. {
  238.     register int    i;
  239.     register long    sum, recsum;
  240.     register char    *p;
  241.     register union record *header;
  242.     long    from_oct();
  243.  
  244.     header = findrec();
  245.     head = header;        /* This is our current header */
  246.     if (NULL == header) return EOF;
  247.     recsum = from_oct(8,  header->header.chksum);
  248.     sum = 0;
  249.     p = header->charptr;
  250.     for (i = sizeof(*header); --i >= 0;) {
  251.         /*
  252.          * We can't use unsigned char here because of old compilers,
  253.          * e.g. V7.
  254.          */
  255.         sum += 0xFF & *p++;
  256.     }
  257.  
  258.     /* Adjust checksum to count the "chksum" field as blanks. */
  259.     for (i = sizeof(header->header.chksum); --i >= 0;)
  260.         sum -= 0xFF & header->header.chksum[i];
  261.     sum += ' '* sizeof header->header.chksum;    
  262.  
  263.     if (sum == recsum) {
  264.         /*
  265.          * Good record.  Decode file size and return.
  266.          */
  267.         if (header->header.linkflag == LF_LINK)
  268.             hstat.st_size = 0;    /* Links 0 size on tape */
  269.         else
  270.             hstat.st_size = from_oct(1+12, header->header.size);
  271.         return 1;
  272.     }
  273.  
  274.     if (sum == 8*' ') {
  275.         /*
  276.          * This is a zeroed record...whole record is 0's except
  277.          * for the 8 blanks we faked for the checksum field.
  278.          */
  279.         return 2;
  280.     }
  281.  
  282.     return 0;
  283. }
  284.  
  285.  
  286. /* 
  287.  * Decode things from a file header record into a "struct stat".
  288.  * Also set "*stdp" to !=0 or ==0 depending whether header record is "Unix
  289.  * Standard" tar format or regular old tar format.
  290.  *
  291.  * read_header() has already decoded the checksum and length, so we don't.
  292.  *
  293.  * If wantug != 0, we want the uid/group info decoded from Unix Standard
  294.  * tapes (for extraction).  If == 0, we are just printing anyway, so save time.
  295.  *
  296.  * decode_header should NOT be called twice for the same record, since the
  297.  * two calls might use different "wantug" values and thus might end up with
  298.  * different uid/gid for the two calls.  If anybody wants the uid/gid they
  299.  * should decode it first, and other callers should decode it without uid/gid
  300.  * before calling a routine, e.g. print_header, that assumes decoded data.
  301.  */
  302. decode_header(header, st, stdp, wantug)
  303.     register union record    *header;
  304.     register struct stat    *st;
  305.     int    *stdp;
  306.     int    wantug;
  307. {
  308.  
  309.     long from_oct();
  310.  
  311.     st->st_mode = from_oct(8,  header->header.mode);
  312.     st->st_mtime = from_oct(1+12, header->header.mtime);
  313.     if(f_gnudump) {
  314.         st->st_atime = from_oct(1+12, header->header.atime);
  315.         st->st_ctime = from_oct(1+12, header->header.ctime);
  316.     }
  317.     
  318.     if (0==strcmp(header->header.magic, TMAGIC)) {
  319.         /* Unix Standard tar archive */
  320.         *stdp = 1;
  321.         if (wantug) {
  322. #ifdef NONAMES
  323.             st->st_uid = from_oct(8,  header->header.uid);
  324.             st->st_gid = from_oct(8,  header->header.gid);
  325. #else
  326.             st->st_uid = finduid(header->header.uname);
  327.             st->st_gid = findgid(header->header.gname);
  328. #endif
  329.         }
  330.         switch (header->header.linkflag) {
  331.         case LF_BLK: case LF_CHR:
  332.             st->st_rdev = makedev(from_oct(8, header->header.devmajor),
  333.                        from_oct(8, header->header.devminor));
  334.         }
  335.     } else {
  336.         /* Old fashioned tar archive */
  337.         *stdp = 0;
  338.         st->st_uid = from_oct(8,  header->header.uid);
  339.         st->st_gid = from_oct(8,  header->header.gid);
  340.         st->st_rdev = 0;
  341.     }
  342. }
  343.  
  344.  
  345. /*
  346.  * Quick and dirty octal conversion.
  347.  *
  348.  * Result is -1 if the field is invalid (all blank, or nonoctal).
  349.  */
  350. long
  351. from_oct(digs, where)
  352.     register int    digs;
  353.     register char    *where;
  354. {
  355.     register long    value;
  356.  
  357.     while (isspace(*where)) {        /* Skip spaces */
  358.         where++;
  359.         if (--digs <= 0)
  360.             return -1;        /* All blank field */
  361.     }
  362.     value = 0;
  363.     while (digs > 0 && isodigit(*where)) {    /* Scan til nonoctal */
  364.         value = (value << 3) | (*where++ - '0');
  365.         --digs;
  366.     }
  367.  
  368.     if (digs > 0 && *where && !isspace(*where))
  369.         return -1;            /* Ended on non-space/nul */
  370.  
  371.     return value;
  372. }
  373.  
  374.  
  375. /*
  376.  * Actually print it.
  377.  *
  378.  * Plain and fancy file header block logging.
  379.  * Non-verbose just prints the name, e.g. for "tar t" or "tar x".
  380.  * This should just contain file names, so it can be fed back into tar
  381.  * with xargs or the "-T" option.  The verbose option can give a bunch
  382.  * of info, one line per file.  I doubt anybody tries to parse its
  383.  * format, or if they do, they shouldn't.  Unix tar is pretty random here
  384.  * anyway.
  385.  *
  386.  * Note that print_header uses the globals <head>, <hstat>, and
  387.  * <head_standard>, which must be set up in advance.  This is not very clean
  388.  * and should be cleaned up.  FIXME.
  389.  */
  390. #define    UGSWIDTH    11        /* min width of User, group, size */
  391. #define    DATEWIDTH    19        /* Last mod date */
  392. static int    ugswidth = UGSWIDTH;    /* Max width encountered so far */
  393.  
  394. void
  395. print_header()
  396. {
  397.     char modes[11];
  398.     char *timestamp;
  399.     char uform[11], gform[11];    /* These hold formatted ints */
  400.     char *user, *group;
  401.     char size[24];        /* Holds a formatted long or maj, min */
  402.     long longie;        /* To make ctime() call portable */
  403.     int    pad;
  404.     char *name;
  405.     extern long baserec;
  406.  
  407.     if(f_sayblock)
  408.         fprintf(msg_file,"rec %10d: ",baserec + (ar_record - ar_block));
  409.     /* annofile(msg_file, (char *)NULL); */
  410.  
  411.     if (f_verbose <= 1) {
  412.         /* Just the fax, mam. */
  413.         char *name;
  414.         name=quote_copy_string(current_filename);
  415.         if(name==0)
  416.             name = current_filename;
  417.         fprintf(msg_file, "%s\n", name);
  418.         if(name!=current_filename)
  419.             free(name);
  420.     } else {
  421.         /* File type and modes */
  422.         modes[0] = '?';
  423.         switch (head->header.linkflag) {
  424.         case LF_VOLHDR:
  425.             modes[0]='V';
  426.             break;
  427.  
  428.         case LF_MULTIVOL:
  429.             modes[0]='M';
  430.             break;
  431.  
  432.         case LF_SPARSE:
  433.         case LF_NORMAL:
  434.         case LF_OLDNORMAL:
  435.         case LF_LINK:
  436.             modes[0] = '-';
  437.             if ('/' == current_filename[strlen(current_filename)-1])
  438.                 modes[0] = 'd';
  439.         break;
  440.  
  441.  
  442.         case LF_DUMPDIR:modes[0] = 'd'; break;
  443.         case LF_DIR:    modes[0] = 'd'; break;
  444.         case LF_SYMLINK:modes[0] = 'l'; break;
  445.         case LF_BLK:    modes[0] = 'b'; break;
  446.         case LF_CHR:    modes[0] = 'c'; break;
  447.         case LF_FIFO:    modes[0] = 'p'; break;    
  448.         case LF_CONTIG:    modes[0] = 'C'; break;
  449.         }
  450.  
  451.         demode((unsigned)hstat.st_mode, modes+1);
  452.  
  453.         /* Timestamp */
  454.         longie = hstat.st_mtime;
  455.         timestamp = ctime(&longie);
  456.         timestamp[16] = '\0';
  457.         timestamp[24] = '\0';
  458.  
  459.         /* User and group names */
  460.         if (*head->header.uname && head_standard) {
  461.             user  = head->header.uname;
  462.         } else {
  463.             user = uform;
  464.             (void)sprintf(uform, "%d", (int)hstat.st_uid);
  465.         }
  466.         if (*head->header.gname && head_standard) {
  467.             group = head->header.gname;
  468.         } else {
  469.             group = gform;
  470.             (void)sprintf(gform, "%d", (int)hstat.st_gid);
  471.         }
  472.  
  473.         /* Format the file size or major/minor device numbers */
  474.         switch (head->header.linkflag) {
  475.         case LF_CHR:
  476.         case LF_BLK:
  477.             (void)sprintf(size, "%d,%d",
  478.                     major(hstat.st_rdev),
  479.                     minor(hstat.st_rdev));
  480.             break;
  481.         case LF_SPARSE:
  482.             (void)sprintf(size, "%ld",
  483.                  from_oct(1+12, head->header.realsize));
  484.             break;
  485.         default:
  486.             (void)sprintf(size, "%ld", (long)hstat.st_size);
  487.         }
  488.  
  489.         /* Figure out padding and print the whole line. */
  490.         pad = strlen(user) + strlen(group) + strlen(size) + 1;
  491.         if (pad > ugswidth) ugswidth = pad;
  492.  
  493.         name = quote_copy_string(current_filename);
  494.         if(!name)
  495.             name=current_filename;
  496.         fprintf(msg_file, "%s %s/%s %*s%s %s %s %s",
  497.             modes,
  498.             user,
  499.             group,
  500.             ugswidth - pad,
  501.             "",
  502.             size,
  503.             timestamp+4, timestamp+20,
  504.             name);
  505.  
  506.         if(name!=current_filename)
  507.             free(name);
  508.         switch (head->header.linkflag) {
  509. #ifdef LF_RMTLINK
  510.         case LF_RMTLINK:
  511. #endif
  512.         case LF_SYMLINK:
  513.             name = quote_copy_string(current_linkname);
  514.             if(!name)
  515.                 name=current_linkname;
  516.             fprintf(msg_file, " -> %s\n", name);
  517.             if(name!=current_linkname)
  518.                 free(name);
  519.             break;
  520.  
  521.         case LF_LINK:
  522.             name = quote_copy_string(current_linkname);
  523.             if(!name)
  524.                 name=current_linkname;
  525.             fprintf(msg_file, " link to %s\n", name);
  526.             if(name!=current_linkname)
  527.                 free(name);
  528.             break;
  529.  
  530.         default:
  531.             fprintf(msg_file, " unknown file type '%c'\n",
  532.                 head->header.linkflag);
  533.             break;
  534.  
  535.         case LF_OLDNORMAL:
  536.         case LF_NORMAL:
  537.         case LF_SPARSE:
  538.         case LF_CHR:
  539.         case LF_BLK:
  540.         case LF_DIR:
  541.         case LF_FIFO:
  542.         case LF_CONTIG:
  543.             case LF_DUMPDIR:
  544. #ifdef LF_PSEUDODEV
  545.         case LF_PSEUDODEV:
  546. #endif
  547.             putc('\n', msg_file);
  548.             break;
  549.  
  550.         case LF_VOLHDR:
  551.             fprintf(msg_file, "--Volume Header--\n");
  552.             break;
  553.             
  554.         case LF_MULTIVOL:
  555.             fprintf(msg_file, "--Continued at byte %ld--\n",from_oct(1+12,head->header.offset));
  556.             break;
  557.         }
  558.     }
  559.     fflush(msg_file);
  560. }
  561.  
  562. /*
  563.  * Print a similar line when we make a directory automatically.
  564.  */
  565. void
  566. pr_mkdir(pathname, length, mode)
  567.     char *pathname;
  568.     int length;
  569.     int mode;
  570. {
  571.     char modes[11];
  572.     char *name;
  573.     extern long baserec;
  574.  
  575.     if (f_verbose > 1) {
  576.         /* File type and modes */
  577.         modes[0] = 'd';
  578.         demode((unsigned)mode, modes+1);
  579.  
  580.         if(f_sayblock)
  581.             fprintf(msg_file,"rec %10d: ",baserec + (ar_record - ar_block));
  582.         /* annofile(msg_file, (char *)NULL); */
  583.         name=quote_copy_string(pathname);
  584.         if(!name)
  585.             name=pathname;
  586.         fprintf(msg_file, "%s %*s %.*s\n",
  587.             modes,
  588.             ugswidth+DATEWIDTH,
  589.             "Creating directory:",
  590.             length,
  591.             pathname);
  592.         if(name!=pathname)
  593.             free(name);
  594.     }
  595. }
  596.  
  597.  
  598. /*
  599.  * Skip over <size> bytes of data in records in the archive.
  600.  */
  601. void
  602. skip_file(size)
  603.     register long size;
  604. {
  605.     union record *x;
  606.     extern long save_totsize;
  607.     extern long save_sizeleft;
  608.  
  609.     if(f_multivol) {
  610.         save_totsize=size;
  611.         save_sizeleft=size;
  612.     }
  613.  
  614.     while (size > 0) {
  615.         x = findrec();
  616.         if (x == NULL) {    /* Check it... */
  617.             msg("Unexpected EOF on archive file");
  618.             exit(EX_BADARCH);
  619.         }
  620.         userec(x);
  621.         size -= RECORDSIZE;
  622.         if(f_multivol)
  623.             save_sizeleft-=RECORDSIZE;
  624.     }
  625. }
  626.  
  627.  
  628. /*
  629.  *----------------------------------------------------------------------
  630.  *
  631.  * skip_extended_headers --
  632.  *
  633.  *    Skip over any extended header records in the archive.
  634.  *
  635.  * Results:
  636.  *    None.
  637.  *
  638.  * Side effects:
  639.  *    The archive is repositioned past any extended header records.
  640.  *
  641.  *----------------------------------------------------------------------
  642.  */
  643.  
  644. void
  645. skip_extended_headers()
  646. {
  647.     register union record *exhdr;
  648.  
  649.     assert(gnu_extended_header(head));
  650.     assert((head->header.isextended
  651.         & (XH_FILENAME|XH_LINKNAME|XH_SPARSE_FILE)) != 0);
  652.     assert((head->header.isextended
  653.         & ~(XH_FILENAME|XH_LINKNAME|XH_SPARSE_FILE)) == 0);
  654.  
  655.     /* 
  656.      * get_names should have already skipped over extended name 
  657.      * records, so the only thing to skip over would be sparse file 
  658.      * records.  (XXX This is a hack.)
  659.      */
  660.      if (head->header.isextended & XH_SPARSE_FILE) {
  661.          for (;;) {
  662.              exhdr = findrec();
  663.              userec(exhdr);
  664.              if (!exhdr->ext_hdr.xh_isextended) {
  665.                  break;
  666.              }
  667.          }
  668.      }
  669. }
  670.  
  671. /*
  672.  * Decode the mode string from a stat entry into a 9-char string and a null.
  673.  */
  674. void
  675. demode(mode, string)
  676.     register unsigned mode;
  677.     register char *string;
  678. {
  679.     register unsigned mask;
  680.     register char *rwx = "rwxrwxrwx";
  681.  
  682.     for (mask = 0400; mask != 0; mask >>= 1) {
  683.         if (mode & mask)
  684.             *string++ = *rwx++;
  685.         else {
  686.             *string++ = '-';
  687.             rwx++;
  688.         }
  689.     }
  690.  
  691.     if (mode & S_ISUID)
  692.         if (string[-7] == 'x')
  693.             string[-7] = 's';
  694.         else
  695.             string[-7] = 'S';
  696.     if (mode & S_ISGID)
  697.         if (string[-4] == 'x')
  698.             string[-4] = 's';
  699.         else
  700.             string[-4] = 'S';
  701.     if (mode & S_ISVTX)
  702.         if (string[-1] == 'x')
  703.             string[-1] = 't';
  704.         else
  705.             string[-1] = 'T';
  706.     *string = '\0';
  707. }
  708.  
  709.  
  710. /*
  711.  *----------------------------------------------------------------------
  712.  *
  713.  * get_names --
  714.  *
  715.  *    Get the file and link names from an archive header.  Assumes that 
  716.  *    the archive has just been advanced past the record for the given 
  717.  *    header.
  718.  *
  719.  * Results:
  720.  *    None.
  721.  *
  722.  * Side effects:
  723.  *    Sets current_filename and current_linkname.  Advances the archive 
  724.  *    past any extended header records.
  725.  *
  726.  *----------------------------------------------------------------------
  727.  */
  728.  
  729. void
  730. get_names(header)
  731.     union record *header;
  732. {
  733.     static char filename_buf[MAXPATHLEN];
  734.     static char linkname_buf[MAXPATHLEN];
  735.  
  736.     assert(header == head);
  737.     assert((head->header.isextended &
  738.         ~(XH_FILENAME|XH_LINKNAME|XH_SPARSE_FILE)) == 0);
  739.     if (head->header.isextended) {
  740.         assert(gnu_extended_header(head));
  741.     }
  742.  
  743.     /* 
  744.      * If there are no extended header records, just use what's in 
  745.      * the regular header.  Otherwise, pull the name out of the 
  746.      * extended header record(s).  Note that it is important that 
  747.      * get_short_name copy the string, rather than just save a pointer. 
  748.      * This is because the buffer package can relocate the header 
  749.      * record when get_long_name is called.
  750.      * 
  751.      * For each set of extended records, copy out the name into the 
  752.      * buffer.  Assume that the file name comes before the link 
  753.      * name (XXX).
  754.      */
  755.     
  756.     if (head->header.isextended & XH_FILENAME) {
  757.         get_long_name(XH_FILENAME, MAXPATHLEN, filename_buf);
  758.     } else {
  759.         get_short_name(NAMSIZ, head->header.name, MAXPATHLEN,
  760.                    filename_buf);
  761.     }
  762.     if (head->header.isextended & XH_LINKNAME) {
  763.         get_long_name(XH_LINKNAME, MAXPATHLEN, linkname_buf);
  764.     } else {
  765.         get_short_name(NAMSIZ, head->header.linkname, MAXPATHLEN,
  766.                    linkname_buf);
  767.     }
  768.  
  769.     if (current_filename) {
  770.         free(current_filename);
  771.     }
  772.     if (current_linkname) {
  773.         free(current_linkname);
  774.     }
  775.     current_filename = strdup(filename_buf);
  776.     current_linkname = strdup(linkname_buf);
  777.     return;
  778. }
  779.  
  780.  
  781. /*
  782.  *----------------------------------------------------------------------
  783.  *
  784.  * get_long_name --
  785.  *
  786.  *    Get one name from one or more extended header records.
  787.  *
  788.  * Results:
  789.  *    Fills in the given buffer.  
  790.  *
  791.  * Side effects:
  792.  *    Advances the archive.  Can cause a saved record from the archive to
  793.  *    get moved.
  794.  *
  795.  *----------------------------------------------------------------------
  796.  */
  797.  
  798. void
  799. get_long_name(type, buflen, buf)
  800.     int type;        /* expected name type */
  801.     int buflen;        /* number of characters in buf */
  802.     char *buf;        /* buffer to fill in */
  803. {
  804.     union record *exhdr;    /* ptr to extended header */
  805.  
  806.     buf[0] = '\0';
  807.  
  808.     for (;;) {
  809.         exhdr = findrec();
  810.         assert(exhdr->ext_hdr.xh_magic == XH_MAGIC);
  811.         assert(exhdr->ext_hdr.xh_type == type);
  812.         assert(strlen(buf) + strlen(exhdr->ext_hdr.xh_namebuf)
  813.                < buflen);
  814.         strcat(buf, exhdr->ext_hdr.xh_namebuf);
  815.         userec(exhdr);
  816.         if (!exhdr->ext_hdr.xh_isextended) {
  817.             break;
  818.         }
  819.     }
  820.  
  821.     assert(strlen(buf) < buflen);
  822. }
  823.  
  824.  
  825. /*
  826.  *----------------------------------------------------------------------
  827.  *
  828.  * get_short_name --
  829.  *
  830.  *    Get a "short" name from a header record.
  831.  *
  832.  * Results:
  833.  *    Fills in the "to" buffer with a null-terminated string.
  834.  *
  835.  * Side effects:
  836.  *    None.
  837.  *
  838.  *----------------------------------------------------------------------
  839.  */
  840.  
  841. void
  842. get_short_name(fromsize, frombuf, tosize, tobuf)
  843.     int fromsize;        /* number of bytes in input buffer */
  844.     char *frombuf;        /* buffer to copy from; maybe not 
  845.                  * null-terminated */
  846.     int tosize;        /* number of bytes in output buffer*/
  847.     char *tobuf;        /* buffer to copy to */
  848. {
  849.     assert(tosize >= fromsize + 1);
  850.  
  851.     strncpy(tobuf, frombuf, fromsize);
  852.     tobuf[fromsize] = '\0';
  853. }
  854.